package cn.org.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.domain.Org;
import cn.org.domain.Person;
import cn.org.service.IOrgService;
import cn.org.service.IPersonService;
import cn.org.utils.ComparatorOrg;
import cn.org.utils.JsonUtils;
/*
 * 部门管理控制
 */

@Controller
public class OrgController {
	
	@Resource
	private IOrgService orgService;
	
	@Resource
	private IPersonService personService;
	
	@RequestMapping("/orglist")
	public String listOrg(HttpSession session,Model model){
		////System.out.println("------orglist-------");
		Person user=(Person) session.getAttribute("user");
		List<Org> childList=new ArrayList<Org>();
		List<Org> list=orgService.getOrgByPerson(user);
		for(Org o:list){
			childList.addAll(orgService.getAllChildOrg(o));
		}
		////System.out.println(list.get(0));
		session.setAttribute("org", list.get(0));
		model.addAttribute("orglist",childList);
		////System.out.println(JsonUtils.list2json(childList));
		
		return "department";
	}
	@RequestMapping("/comorglist")
	public String listcomOrg(HttpSession session,Model model){
		////System.out.println("------orglist-------");
		Person user=(Person) session.getAttribute("user");
		List<Org> childList=new ArrayList<Org>();
		List<Org> list=orgService.getOrgByPerson(user);
		for(Org o:list){
			childList.addAll(orgService.getAllChildOrg(o));
		}
		////System.out.println(list.get(0));
		session.setAttribute("org", list.get(0));
		model.addAttribute("orglist",childList);
		////System.out.println(JsonUtils.list2json(childList));
		
		return "departmentshow";
	}
	@RequestMapping("/childorglist")
	public String listchildOrg(@RequestParam("oid") int oid,HttpSession session,Model model){
		Org o=orgService.selectByPrimaryKey(oid);
		session.setAttribute("org", o);
		////System.out.println(o);
		////System.out.println(JsonUtils.list2json(childList));
		
		return "department";
	}
	
	@RequestMapping("/indexchildorglist")
	public String indexlistchildOrg(@RequestParam("oid") int oid,HttpSession session,Model model){
		Org o=orgService.selectByPrimaryKey(oid);
		session.setAttribute("org", o);
		////System.out.println(o);
		////System.out.println(JsonUtils.list2json(childList));
		
		return "showdepartment";
	}
	@RequestMapping("/comindexchildorglist")
	public String comindexlistchildOrg(@RequestParam("oid") int oid,HttpSession session,Model model){
		Org o=orgService.selectByPrimaryKey(oid);
		session.setAttribute("org", o);
		////System.out.println(o);
		////System.out.println(JsonUtils.list2json(childList));
		
		return "departmentshow";
	}
	@RequestMapping(value="/listorg",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListOrg(HttpSession session){
		Org org=(Org) session.getAttribute("org");
		////System.out.println("---------ListOrg-----------");
		////System.out.println(org);
		List<Org> childList=new ArrayList<Org>();
		childList=getChildOrgs(childList,org);
		ComparatorOrg comparatorOrg=new ComparatorOrg();
		Collections.sort(childList, comparatorOrg);
		////System.out.println(JsonUtils.list2json(childList));
		////System.out.println(childList.size());
		String json="[";
		for(int i=0;i<childList.size();i++){
			Org o=childList.get(i);
			Org porg=orgService.selectByPrimaryKey(o.getPid());
			if(i<childList.size()-1)
			 json=json+"{\"address\":\""+o.getAddress()+"\",\"descId\":\""+o.getDescId()+"\",\"id\":\""+o.getId()+"\",\"level\":\""+o.getLevel()+"\",\"name\":\""+o.getName()+"\",\"phone\":\""+o.getPhone()+"\",\"porg\":\""+porg.getName()+"\",\"pid\":\""+o.getPid()+"\",\"sn\":\""+o.getSn()+"\"},";
			else
				 json=json+"{\"address\":\""+o.getAddress()+"\",\"descId\":\""+o.getDescId()+"\",\"id\":\""+o.getId()+"\",\"level\":\""+o.getLevel()+"\",\"name\":\""+o.getName()+"\",\"phone\":\""+o.getPhone()+"\",\"porg\":\""+porg.getName()+"\",\"pid\":\""+o.getPid()+"\",\"sn\":\""+o.getSn()+"\"}";
			
		}
		json=json+"]";
		////System.out.println("listorg:--------->"+json);
		return json;
	}
	@RequestMapping(value="/listorgtree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListOrgTree(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		Org org=orgService.getOrgByPerson(user).get(0);
		////System.out.println(org);
		////System.out.println("---------ListOrgTree-----------");
		List<Org> childList=new ArrayList<Org>();
		childList=getChildOrgs(childList,org);
		org.setPid(0);
		childList.add(org);
		
	
		//System.out.println(JsonUtils.OrgList2Tree(childList));
		return JsonUtils.OrgList2Tree(childList);
	}
	
	@RequestMapping(value="/listindexorgtree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListIndexOrgTree(){
		Org org=orgService.selectByName("宁波市");
		////System.out.println(org);
		////System.out.println("---------ListOrgTree-----------");
		List<Org> childList=new ArrayList<Org>();
		childList=getChildOrgs(childList,org);
		org.setPid(0);
		childList.add(org);
		
	
		//System.out.println(JsonUtils.OrgList2Tree(childList));
		return JsonUtils.IndexOrgList2Tree(childList);
	}
	@RequestMapping(value="/listcomindexorgtree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListComIndexOrgTree(){
		Org org=orgService.selectByName("宁波市");
		////System.out.println(org);
		////System.out.println("---------ListOrgTree-----------");
		List<Org> childList=new ArrayList<Org>();
		childList=getChildOrgs(childList,org);
		org.setPid(0);
		childList.add(org);
		
	
		//System.out.println(JsonUtils.OrgList2Tree(childList));
		return JsonUtils.ComIndexOrgList2Tree(childList);
	}
	
	@RequestMapping(value="/listorgpersontree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListOrgPersonTree(HttpSession session){
		Org org=(Org) session.getAttribute("org");
		////System.out.println("---------ListOrgPersonTree-----------");
		List<Org> childorgList=new ArrayList<Org>();
		childorgList=getChildOrgs(childorgList,org);
		org.setPid(0);
		childorgList.add(org);
		List<Person> childpersonList=new ArrayList<Person>();
		childpersonList=getChildPersons(childpersonList,org);
		

	
		////System.out.println(PersonOrgList2Tree(childpersonList,childorgList));
		return PersonOrgList2Tree(childpersonList,childorgList);
	}
	
	@RequestMapping(value="/addorg",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String AddOrg(Org org){
		//System.out.println(org);
		orgService.insert(org);
		/*
		 * 添加部门数据库操作
		 */
		return "{\"data\":\"success\"}";
	}
	@RequestMapping(value="/getparentorg",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String GetParentOrg(HttpSession session){
		Org org=(Org) session.getAttribute("org");
		List<Org> list=new ArrayList<Org>();
		list.add(org);
		list=getChildOrgs(list,org);
		return JsonUtils.list2json(list);
	}
	
	@RequestMapping(value="/editorg",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String EditOrg(@RequestParam("oid") int oid){
		Org org=orgService.selectByPrimaryKey(oid);
		//System.out.println(org);
		Org porg=orgService.selectByPrimaryKey(org.getPid());
		/* String orglevel="一级行政机构";
		 int level=org.getLevel();
		  if(level==1) orglevel="一级行政机构";
		  else if(level==2) orglevel="二级行政机构";
		  else if(level==3) orglevel="三级行政机构";
		  else if(level==4) orglevel="四级行政机构";
		  else orglevel="特殊行政机构";*/
		String json="{\"address\":\""+org.getAddress()+"\",\"descId\":\""+org.getDescId()+"\",\"id\":\""+org.getId()+"\",\"level\":\""+org.getLevel()+"\",\"name\":\""+org.getName()+"\",\"phone\":\""+org.getPhone()+"\",\"porg\":\""+porg.getName()+"\",\"pid\":\""+org.getPid()+"\",\"sn\":\""+org.getSn()+"\"}";
	//	//System.out.println(json);
		return json;
	}  
	
	@RequestMapping(value="/confirmeditorg",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ConfirmEditOrg(@RequestParam("porg") String porgname,Org org){
		//System.out.println(org);
		/*
		 * 编辑部门数据库操作
		 */
		Org porg=orgService.selectByName(porgname);
		org.setPid(porg.getId());
		orgService.updateByPrimaryKey(org);
		return "{\"data\":\"success\"}";
	}
	
	@RequestMapping(value="/delorg",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String DelOrg(@RequestParam("oid") int oid){
		Org org=orgService.selectByPrimaryKey(oid);
		//System.out.println(org);
		/*
		 * 删除部门数据库操作
		 */
		orgService.deleteByPrimaryKey(oid);
		return "{\"data\":\"success\"}";
	}  
	private List<Org> getChildOrgs(List<Org> resultlist,Org o ){
		List<Org> list=orgService.getAllChildOrg(o);
		if(!list.isEmpty()){
		for(Org org:list){
		resultlist.add(org);
		 getChildOrgs(resultlist,org);	
		}
		}
		return resultlist;
	}
	
	private List<Person> getChildPersons(List<Person> resultlist,Org o ){
		List<Org> list=orgService.getAllChildOrg(o);
		resultlist.addAll(personService.getPersonsByOrg(o));
		if(!list.isEmpty()){
		for(Org org:list){
		
		//resultlist.addAll(personService.getPersonsByOrg(org));
		getChildPersons(resultlist,org);	
		}
		}
		return resultlist;
	}
	private  String PersonOrgList2Tree(List<Person> plist,List<Org> olist){
		String tree="[";
		for(int i=0;i<plist.size();i++){
			Person person=plist.get(i);
		tree=tree+"{\"id\":"+person.getId()+", \"pId\":"+orgService.getOrgByPerson(person).get(0).getId()+", \"name\":\""+person.getName()+"\", \"icon\":\"./css/zTreeStyle/img/diy/user.png\"},";
				}
		for(int i=0;i<olist.size();i++){
			Org org=olist.get(i);
			if(i<olist.size()-1)
				  tree=tree+"{\"id\":"+org.getId()+", \"pId\":"+org.getPid()+", \"name\":\""+org.getName()+"\", \"url\":\"childorglist?oid="+org.getId()+"\",\"target\":\"_self\",\"open\":true, \"icon\":\"./css/zTreeStyle/img/diy/1_open.png\", \"iconClose\":\"./css/zTreeStyle/img/diy/1_close.png\"},";
				else tree=tree+"{\"id\":"+org.getId()+", \"pId\":"+org.getPid()+", \"name\":\""+org.getName()+"\", \"url\":\"childorglist?oid="+org.getId()+"\",\"target\":\"_self\",\"open\":true, \"icon\":\"./css/zTreeStyle/img/diy/1_open.png\", \"iconClose\":\"./css/zTreeStyle/img/diy/1_close.png\"}";
				}
				tree=tree+"]";
		return tree;
		
	}
}
